//
//  KJPopView.m
//  AnItemForACar
//
//  Created by Jack on 16/8/30.
//  Copyright © 2016年 shiying. All rights reserved.
//

#import "KJPopView.h"
#import "KJLabel.h"
#import "KJButton.h"

@implementation KJPopView

#pragma mark - 弹出视图
- (instancetype)initWithImage:(UIImage *)image Title:(NSString *)title subTitle:(NSString *)subTitle buttonName:(NSString *)name {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        
        
        //白色底图
        UIView * bacView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 235)];
        bacView.center = CGPointMake(self.width / 2, self.height / 2 - 50);
        bacView.layer.cornerRadius = 10;
        bacView.backgroundColor = [UIColor whiteColor];
        
        //图片
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        imageView.image = [UIImage imageNamed:@"成功"];
        imageView.center = CGPointMake(bacView.width / 2, 20);
        imageView.top = 20;
        [bacView addSubview:imageView];
        
        //标题
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bacView.width, 17)];
        titleLabel.text = title;
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor colorWithRed:0.102 green:0.098 blue:0.098 alpha:1.000];
        titleLabel.top = imageView.bottom + 20;
        [bacView addSubview:titleLabel];
        
        //子标题
        KJLabel * subLabel = [[KJLabel alloc] initWithFrame:CGRectMake(0, titleLabel.bottom + 10, bacView.width, kFontSysSize) text:subTitle textColor:[[UIColor colorWithRed:0.102 green:0.098 blue:0.098 alpha:1.000] colorWithAlphaComponent:0.8]];
        [bacView addSubview:subLabel];
        
        KJButton * button = [KJButton buttonWithFrame:CGRectMake((bacView.width - 140) / 2, subLabel.bottom + 20, 140, 35) title:name];
        [bacView addSubview:button];
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:bacView];
        
        self.tag = 1314;
    }
    return self;
}

+ (instancetype)KJShowPopViewWithImage:(UIImage *)image Title:(NSString *)title subTitle:(NSString *)subTitle buttonName:(NSString *)name {
    KJPopView * view = [[self alloc] initWithImage:image Title:title subTitle:subTitle buttonName:name];
        //show View
    UIWindow * window = [[UIApplication sharedApplication].delegate window];
    [window addSubview:view];
    
    return view;
}






#pragma mark - 弹出警告视图
- (instancetype)initWithWarmImage:(UIImage *)image Title:(NSString *)string {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];

        UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 210, 90)];
        bgView.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.5];
        bgView.layer.cornerRadius = 2;
        bgView.center = CGPointMake(self.width / 2, self.height / 2 - 50);

        
        
        //图片
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        imageView.image = image;
        imageView.centerX = bgView.width / 2;
        imageView.centerY = imageView.height / 2  + 10;
        [bgView addSubview:imageView];
        
        //文字
        KJLabel * label = [[KJLabel alloc] initWithFrame:CGRectMake(0, bgView.height / 2, bgView.width, bgView.height / 2) text:string textColor:[UIColor whiteColor]];
        label.font = [UIFont systemFontOfSize:kFontSysSize];
        [bgView addSubview:label];
        
        self.tag = 1314;
        
        [self performSelector:@selector(buttonAction:) withObject:nil afterDelay:1];
        
        [self addSubview:bgView];
    }
    return self;
}

+ (instancetype)KJShowWarmImage:(UIImage *)image Title:(NSString *)string {
    KJPopView * view = [[self alloc] initWithWarmImage:image Title:string];
    
    UIWindow * window = [[UIApplication sharedApplication].delegate window];
    [window addSubview:view];
    
    return view;
}

#pragma mark - 快速使用
+ (void)KJShowWarmWithTitle:(NSString *)title {
    [self KJShowWarmImage:[UIImage imageNamed:@"warm"] Title:title];
}
+ (void)KJShowCompleteWarm:(NSString *)title {
    [self KJShowWarmImage:[UIImage imageNamed:@"icon_fasongchenggong"] Title:title];

}

#pragma mark - 事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch=[touches anyObject];
    UIView * view = touch.view;
    if (view.tag == 1314) {
        [self removeFromSuperview];
    }
}

- (void)buttonAction:(UIButton *)sender {
    [self removeFromSuperview];
}

@end
